import Vue from 'vue'
import VueRouter from 'vue-router'
//导入登录组件
import Login from '../components/Login.vue'
//导入home主页组件
import Home from '../components/Home.vue'
//导入welcome组件
import Welcome from '../components/Welcome.vue'
//导入用户列表组件
import Users from '../components/user/Users.vue'
//导入权限列表组件
import Rights from '../components/power/Rights.vue'
//导入角色列表
import Roles from '../components/power/Roles.vue'
//导入商品分类列表组件
import Cate from '../components/goods/Cate.vue'
//导入分类参数列表组件
import Params from '../components/goods/Params.vue'
//导入商品列表组件
import List from '../components/goods/List.vue'
//导入添加商品组件
import Add from '../components/goods/Add.vue'
//导入订单列表组件
import Order from '../components/goods/Order.vue'
//导入数据报表页面组件
import Report from '../components/goods/Report.vue'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/', redirect: '/login'
    },
    {
      path: '/login', component: Login
    },
    {
      path: '/home', component: Home, redirect: '/welcome', children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path:'/roles',component:Roles},
        {path:'/categories',component:Cate},
        {path:'/params',component:Params},
        {path:'/goods',component:List},
        {path:'/goods/add',component:Add},
        {path:'/orders',component:Order},
        {path:'/reports',component:Report}
      ]
    }
  ]
})
//挂载路由导航守卫,(如果用户没有登录，但是直接通过URL访问特定页面，需要重新导航到登录页面)
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) return next('/login');
  next();
})
export default router;

